package com.feeyin.radar.crm.dao.impl;

import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.crm.dao.SaleRecordDAO;
import com.feeyin.radar.crm.model.SaleRecord;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class SaleRecordDAOImpl extends SqlMapClientDaoSupport
  implements SaleRecordDAO
{
  public Integer getCurrentDateRecordCount(SaleRecord saleRecord)
  {
    return (Integer)getSqlMapClientTemplate().queryForObject("getCurrentDateRecordCount", saleRecord);
  }

  public List getSaleRecordsByCustomerId(Integer customerId) {
    if (customerId == null) return null;
    return getSqlMapClientTemplate().queryForList("getSaleRecordsByCustomerId", customerId);
  }

  public List getSaleRecordsByCustomerIdAndStatus(Integer customerId, String status)
  {
    if (customerId == null) return null;
    if (StringUtils.isEmpty(status)) return null;
    SaleRecord saleRecord = new SaleRecord();
    saleRecord.setCustomerId(customerId);
    saleRecord.setStatus(status);
    return getSqlMapClientTemplate().queryForList("getSaleRecordsByDynamic", saleRecord);
  }

  public List getSaleRecordsByCreatePersonId(Integer createPersonId) {
    if (createPersonId == null) return null;
    return getSqlMapClientTemplate().queryForList("getSaleRecordsByCreatePersonId", createPersonId);
  }

  public List getSaleRecordsByCreatePersonIds(String createPersonIds) {
    if (StringUtils.isEmpty(createPersonIds)) return null;
    return getSqlMapClientTemplate().queryForList("getSaleRecordsByCreatePersonIds", createPersonIds);
  }

  public List getSaleRecordsByDeptIds(String deptIds) {
    return getSqlMapClientTemplate().queryForList("getSaleRecordsByDeptIds", deptIds);
  }

  public SaleRecord getSaleRecord(Integer id) {
    return (SaleRecord)getSqlMapClientTemplate().queryForObject("getSaleRecord", id);
  }

  public SaleRecord getSaleRecordBySaleRecordCode(String saleRecordCode) {
    return (SaleRecord)getSqlMapClientTemplate().queryForObject("getSaleRecordBySaleRecordCode", saleRecordCode);
  }

  public SaleRecord getSaleRecordLastLinkByCustomerIdStatus(Integer customerId, String status, Date startDate, Date endDate)
  {
    if (customerId == null) return null;
    if (StringUtils.isEmpty(status)) return null;
    if (startDate == null) return null;
    if (endDate == null) return null;
    SaleRecord saleRecord = new SaleRecord();
    saleRecord.setCustomerId(customerId);
    saleRecord.setStatus(status);
    saleRecord.setStartDate(startDate);
    saleRecord.setEndDate(endDate);
    return (SaleRecord)getSqlMapClientTemplate().queryForObject("getSaleRecordLastLinkByDynamic", saleRecord);
  }

  public SaleRecord getSaleRecordLastLinkByCustomerIdStatus(Integer customerId, String status) {
    if (customerId == null) return null;
    if (StringUtils.isEmpty(status)) return null;
    SaleRecord saleRecord = new SaleRecord();
    saleRecord.setCustomerId(customerId);
    saleRecord.setStatus(status);
    return (SaleRecord)getSqlMapClientTemplate().queryForObject("getSaleRecordLastLinkByDynamic", saleRecord);
  }

  public Date getSaleRecordLastLinkByCustomerId(Integer customerId) {
    if (customerId == null) return null;
    return (Date)getSqlMapClientTemplate().queryForObject("getSaleRecordLastLinkByCustomerId", customerId);
  }

  public Date getSaleRecordLastLinkByCustomerIdAndStatus(Integer customerId, String status)
  {
    if (customerId == null) return null;
    if (StringUtils.isEmpty(status)) return null;
    SaleRecord saleRecord = new SaleRecord();
    saleRecord = getSaleRecordLastLinkByCustomerIdStatus(customerId, status);
    if ((saleRecord != null) && (saleRecord.getContactDate() != null)) return saleRecord.getContactDate();
    return null;
  }

  public Date getSaleRecordMinDateByCustomerIdAndStatus(Integer customerId, String status)
  {
    if (customerId == null) return null;
    if (StringUtils.isEmpty(status)) return null;
    SaleRecord saleRecord = new SaleRecord();
    saleRecord.setCustomerId(customerId);
    saleRecord.setStatus(status);
    return (Date)getSqlMapClientTemplate().queryForObject("getSaleRecordMinDateByDynamic", saleRecord);
  }

  public Integer getSaleRecordCountByCustomerId(Integer customerId) {
    if (customerId == null) return Integer.valueOf(0);
    return (Integer)getSqlMapClientTemplate().queryForObject("getSaleRecordCountByCustomerId", customerId);
  }

  public List getCloseSaleRcordsByCustomerId(Integer customerId) {
    if (customerId == null) return null;
    return getSqlMapClientTemplate().queryForList("getCloseSaleRcordsByCustomerId", customerId);
  }

  public boolean isFileExists(String basePath, String fullPath, Integer customerId, Integer saleRecordId, String fileName)
  {
    String fullPathName = "";
    if (StringUtils.isEmpty(fullPath))
      fullPathName = basePath + "upload" + System.getProperty("file.separator") + 
        "saleRecord" + System.getProperty("file.separator") + 
        customerId + "_" + saleRecordId + System.getProperty("file.separator") + 
        fileName;
    else {
      fullPathName = fullPath + fileName;
    }

    File file = new File(fullPathName);
    return file.exists();
  }

  public boolean insertSaleRecord(SaleRecord saleRecord)
  {
    Integer result = (Integer)getSqlMapClientTemplate().insert("insertSaleRecord", saleRecord);
    return result != null;
  }

  public boolean updateSaleRecord(SaleRecord saleRecord)
  {
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateSaleRecord", saleRecord));
    return result != null;
  }

  public boolean updateSaleRecordCustomerIdById(Integer customerId, Integer saleRecordId)
  {
    if (customerId == null) return false;
    if (saleRecordId == null) return false;
    Map map = new HashMap();
    map.put("customerId", customerId);
    map.put("id", saleRecordId);
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateSaleRecordCustomerIdById", map));
    return result != null;
  }

  public boolean updateSaleRecordStatusByCustomerId(Integer customerId, String currentStatus, String updateStatus)
  {
    if (customerId == null) return false;
    if (StringUtils.isEmpty(currentStatus)) return false;
    if (StringUtils.isEmpty(updateStatus)) return false;
    Map map = new HashMap();
    map.put("customerId", customerId);
    map.put("status", currentStatus);
    map.put("value", updateStatus);
    if ("1".equals(updateStatus)) {
      SaleRecord saleRecord = new SaleRecord();
      saleRecord = getSaleRecordLastLinkByCustomerIdStatus(customerId, currentStatus);
      if ((saleRecord == null) || (saleRecord.getId() == null)) return false;
      map.put("parentId", saleRecord.getId());
    }
    Integer result = Integer.valueOf(getSqlMapClientTemplate().update("updateSaleRecordStatusByCustomerId", map));
    return result != null;
  }

  public boolean deleteSaleRecordByCustomerIdAndStatus(Integer customerId, String status)
  {
    if (customerId == null) return false;
    if (StringUtils.isEmpty(status)) return false;
    Map map = new HashMap();
    map.put("customerId", customerId);
    map.put("status", status);
    Integer result = Integer.valueOf(getSqlMapClientTemplate().delete("deleteSaleRecordByCustomerIdAndStatus", map));
    return result != null;
  }
}